package auto.feature;
import auto.device.Phione;
import io.appium.java_client.*;
import org.openqa.selenium.*;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.interactions.Pause;
import org.openqa.selenium.interactions.PointerInput;
import org.openqa.selenium.interactions.Sequence;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
import java.util.Arrays;
import java.util.Collections;



/*
* 下面点击联系人列表和点击联系人可以封装到一个函数中
*
* 列表发送视频和照片也可封装到一个函数中
* */
public class PrivateSendMessage {

//feature1：发送图片，里面有很多方式发送图片
    public String sendPicture(){
        Phione phione = new Phione();
        AppiumDriver driver = phione.driver;
        WebDriverWait wait = phione.wait;

        //首先点击联系人，进入联系人列表
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain
                ("**/XCUIElementTypeButton[`name == \"联系人\"`]"))).click();

        //点击列表中的第一个用户
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeWindow[1]" +
                "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther[2]/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeTable/XCUIElementTypeCell[5]"))).click();

        //点击附件按钮
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("MessagingApp.ConversationInputToolbar._attachmentButton"))).click();
        }catch(TimeoutException e){
            return "附件按钮消失";
        }

        //选中列表中的图片发送
        sendPictureUseList(driver,wait);

        //点击附件按钮
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("MessagingApp.ConversationInputToolbar._attachmentButton"))).click();
        }catch(TimeoutException e){
            return "附件按钮消失";
        }

        //使用拍摄发送照片
        sendPictureUseCamera(driver,wait);

        //点击附件按钮
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("MessagingApp.ConversationInputToolbar._attachmentButton"))).click();
        }catch(TimeoutException e){
            return "附件按钮消失";
        }

        //使用拍摄内相册发送照片
        sendPictureUseAlbumInCamera(driver,wait);

        //点击附件按钮
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("MessagingApp.ConversationInputToolbar._attachmentButton"))).click();
        }catch(TimeoutException e){
            return "附件按钮消失";
        }

        //使用文件内相册发送照片
        sendPictureUseAlbumInFile(driver,wait);

        //发送完之后返回到主页面
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("icon back blue"))).click();
        return "发送图片正常";
    }


//在列表中发送图片
    private void sendPictureUseList( AppiumDriver driver,WebDriverWait wait){
        WebElement element=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("最近项目")));

        // 获取元素的位置和大小
        Point location = element.getLocation();
        Dimension size = element.getSize();

        // 计算长按的起始点和滑动的终止点
        int startX = location.getX() + size.getWidth() / 2;
        int startY = location.getY() + size.getHeight() / 2;
        int endY = startY - 200; // 向上滑动 200 像素

        // 创建 PointerInput 对象
        PointerInput finger = new PointerInput(PointerInput.Kind.TOUCH, "finger");

        // 创建 Sequence 对象
        Sequence sequence = new Sequence(finger, 1);

        // 添加动作到序列中
        sequence.addAction(finger.createPointerMove(Duration.ZERO, PointerInput.Origin.viewport(), startX, startY));
        sequence.addAction(finger.createPointerDown(PointerInput.MouseButton.LEFT.asArg()));
        sequence.addAction(new org.openqa.selenium.interactions.Pause(finger, Duration.ofMillis(500)));
        sequence.addAction(finger.createPointerMove(Duration.ofMillis(500), PointerInput.Origin.viewport(), startX, endY));
        sequence.addAction(finger.createPointerUp(PointerInput.MouseButton.LEFT.asArg()));

        // 执行动作序列
        driver.perform(Collections.singletonList(sequence));
        //上划展示全部的图片

        //选中第一张图片
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]/XCUIElementTypeOther[2]" +
                "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeCollectionView/XCUIElementTypeCell[2]/XCUIElementTypeOther/XCUIElementTypeButton"))).click();

        //定位发送按钮,点击发送
        WebElement sendButton=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("input bar text nonempty")));
        sendButton.click();
    }

//使用相机拍摄发送图片
    private void sendPictureUseCamera(AppiumDriver driver,WebDriverWait wait){
        //从附件点相机进入
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]" +
                "/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeCollectionView/XCUIElementTypeCell[1]/XCUIElementTypeOther" +
                "/XCUIElementTypeOther"))).click();

        //点击拍照
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeWindow[1]/XCUIElementTypeOther[3]/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther[6]/XCUIElementTypeOther/XCUIElementTypeOther[2]"))).click();

        //拍照之后点击发送
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`name == \"发送\"`][2]"))).click();
    }

//相机内选择相册发送图片
    private void sendPictureUseAlbumInCamera(AppiumDriver driver,WebDriverWait wait){
        //点击附件中的相机
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]" +
                "/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeCollectionView/XCUIElementTypeCell[1]/XCUIElementTypeOther" +
                "/XCUIElementTypeOther"))).click();

        //点击相机内的相册
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeWindow[1]/XCUIElementTypeOther[3]" +
                "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther[6]/XCUIElementTypeButton[1]"))).click();

        //选中弹出的相册中的图片
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeWindow[1]/XCUIElementTypeOther[4]" +
                "/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther[1]/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeCollectionView/XCUIElementTypeCell[1]/XCUIElementTypeOther/XCUIElementTypeButton"))).click();

        //选中之后点击发送
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("input bar text nonempty"))).click();
    }

//在文件内选择照片发送照片
    private void sendPictureUseAlbumInFile(AppiumDriver driver,WebDriverWait wait){
        //点击文件
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("文件"))).click();

        //选择文件内相册
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("从相册中选择"))).click();

        //选中一个照片
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther[1]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeCollectionView/XCUIElementTypeCell[1]/XCUIElementTypeOther/XCUIElementTypeButton"))).click();

        //点击发送按钮
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("input bar text nonempty"))).click();
    }




//feature2：发送视频，各种方式发送
    public String sendVideo() throws InterruptedException {
        Phione phione = new Phione();
        AppiumDriver driver = phione.driver;
        WebDriverWait wait = phione.wait;

        //首先点击联系人，进入联系人列表
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain
                ("**/XCUIElementTypeButton[`name == \"联系人\"`]"))).click();

        //点击列表中的第一个用户
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeWindow[1]" +
                "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther[2]/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeTable/XCUIElementTypeCell[5]"))).click();

        //点击附件按钮
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("MessagingApp.ConversationInputToolbar._attachmentButton"))).click();
        }catch(TimeoutException e){
            return "附件按钮消失";
        }

        //从列表中选中视频
        sendVideoUseList(driver,wait);

        //点击附件按钮
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("MessagingApp.ConversationInputToolbar._attachmentButton"))).click();
        }catch(TimeoutException e){
            return "附件按钮消失";
        }

        //使用摄像机拍摄视频
        sendVideoUseCamera(driver,wait);

        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("MessagingApp.ConversationInputToolbar._attachmentButton"))).click();
        }catch(TimeoutException e){
            return "附件按钮消失";
        }

        //进入相机相册发送视频
        sendVideoUseAlbumInCamera(driver,wait);

        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("MessagingApp.ConversationInputToolbar._attachmentButton"))).click();
        }catch(TimeoutException e){
            return "附件按钮消失";
        }

        //进入文件，使用相册发送视频
        sendVideoUseAlbumInFile(driver,wait);

        return "发送视频正常";
    }


//从列表中选中视频进行发送
    private void sendVideoUseList(AppiumDriver driver,WebDriverWait wait) throws InterruptedException {
        WebElement element=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("最近项目")));

        // 获取元素的位置和大小
        Point location = element.getLocation();
        Dimension size = element.getSize();

        // 计算长按的起始点和滑动的终止点
        int startX = location.getX() + size.getWidth() / 2;
        int startY = location.getY() + size.getHeight() / 2;
        int endY = startY - 200; // 向上滑动 200 像素

        // 创建 PointerInput 对象
        PointerInput finger = new PointerInput(PointerInput.Kind.TOUCH, "finger");

        // 创建 Sequence 对象
        Sequence sequence = new Sequence(finger, 1);

        // 添加动作到序列中
        sequence.addAction(finger.createPointerMove(Duration.ZERO, PointerInput.Origin.viewport(), startX, startY));
        sequence.addAction(finger.createPointerDown(PointerInput.MouseButton.LEFT.asArg()));
        sequence.addAction(new org.openqa.selenium.interactions.Pause(finger, Duration.ofMillis(500)));
        sequence.addAction(finger.createPointerMove(Duration.ofMillis(500), PointerInput.Origin.viewport(), startX, endY));
        sequence.addAction(finger.createPointerUp(PointerInput.MouseButton.LEFT.asArg()));

        // 执行动作序列
        driver.perform(Collections.singletonList(sequence));

        //选中一个视频元素
        WebElement videoElement=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("0:03")));

        System.out.println("开始获取父元素");
        //获取这个视频元素的父元素
        WebElement videoElementParent=driver.findElement(AppiumBy.xpath("//XCUIElementTypeAny[@name=\"0:03\"]/.."));

        System.out.println("开始查找叔叔元素");
        //获取videoElement父元素的同级别元素，并点击
        WebElement videoElementUncle =driver.findElement(AppiumBy.xpath("//XCUIElementTypeAny[@name=\"0:03\"]/../following-sibling::XCUIElementTypeButton | //XCUIElementTypeAny[@name=\"0:03\"]/../preceding-sibling::XCUIElementTypeButton"));
        videoElementUncle.click();

        //定位发送按钮,点击发送
        WebElement sendButton=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("input bar text nonempty")));
        sendButton.click();
    }

//使用摄像机拍摄视频进行发送
    private void sendVideoUseCamera(AppiumDriver driver,WebDriverWait wait){
        //从附件点相机进入
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]" +
                "/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeCollectionView/XCUIElementTypeCell[1]/XCUIElementTypeOther" +
                "/XCUIElementTypeOther"))).click();

        //长按拍照键拍摄视频，长按五秒钟
        WebElement shootButton =wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeWindow[1]/XCUIElementTypeOther[3]/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther[6]/XCUIElementTypeOther/XCUIElementTypeOther[2]")));
        PointerInput finger = new PointerInput(PointerInput.Kind.TOUCH, "finger");
        Sequence longPressShootButton = new Sequence(finger, 1)
                .addAction(finger.createPointerMove(Duration.ZERO, PointerInput.Origin.viewport(), shootButton.getLocation().getX(), shootButton.getLocation().getY()))
                .addAction(finger.createPointerDown(PointerInput.MouseButton.LEFT.asArg()))
                .addAction(new Pause(finger, Duration.ofSeconds(5)))
                .addAction(finger.createPointerUp(PointerInput.MouseButton.LEFT.asArg()));

        driver.perform(Arrays.asList(longPressShootButton));

        //长按之后松开，出现发送按钮后发送视频
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`name == \"发送\"`][2]"))).click();
    }


//使用相机中的相册发送视频
    private void sendVideoUseAlbumInCamera(AppiumDriver driver,WebDriverWait wait){
        //点击附件中的相机
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]" +
                "/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeCollectionView/XCUIElementTypeCell[1]/XCUIElementTypeOther" +
                "/XCUIElementTypeOther"))).click();

        //点击相机内的相册
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeWindow[1]/XCUIElementTypeOther[3]" +
                "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther[6]/XCUIElementTypeButton[1]"))).click();

        System.out.println("定位视频");
        //选中一个视频元素
        WebElement videoElement=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.xpath("(//XCUIElementTypeStaticText[@name=\"0:03\"])[2]")));

        System.out.println("开始获取父元素");
        //获取这个视频元素的父元素
        WebElement videoElementParent=driver.findElement(AppiumBy.xpath("//*[name()=\"XCUIElementTypeStaticText\" and @name=\"0:03\"]/.."));

        System.out.println("开始查找叔叔元素");
        //获取videoElement父元素的同级别元素，并点击
        WebElement videoElementUncle =driver.findElement(AppiumBy.xpath("//*[name()=\"XCUIElementTypeStaticText\" and @name=\"0:03\"]/../following-sibling::XCUIElementTypeButton | //*[name()=\"XCUIElementTypeStaticText\" and @name=\"0:03\"]/../preceding-sibling::XCUIElementTypeButton"));
        videoElementUncle.click();

        //定位发送按钮,点击发送
        WebElement sendButton=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("input bar text nonempty")));
        sendButton.click();
    }

//使用文件中的相册发送视频
    private void sendVideoUseAlbumInFile(AppiumDriver driver,WebDriverWait wait){
        //点击文件
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("文件"))).click();

        //选择文件内相册
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("从相册中选择"))).click();

        //选中一个视频元素
        WebElement videoElement=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("0:03")));

        System.out.println("开始获取父元素");
        //获取这个视频元素的父元素
        WebElement videoElementParent=driver.findElement(AppiumBy.xpath("//XCUIElementTypeAny[@name=\"0:03\"]/.."));

        System.out.println("开始查找叔叔元素");
        //获取videoElement父元素的同级别元素，并点击
        WebElement videoElementUncle =driver.findElement(AppiumBy.xpath("//XCUIElementTypeAny[@name=\"0:03\"]/../following-sibling::XCUIElementTypeButton | //XCUIElementTypeAny[@name=\"0:03\"]/../preceding-sibling::XCUIElementTypeButton"));
        videoElementUncle.click();

        //定位发送按钮,点击发送
        WebElement sendButton=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("input bar text nonempty")));
        sendButton.click();
    }


//feature4：发送文件
    public String sendFile(){
        Phione phione = new Phione();
        AppiumDriver driver = phione.driver;
        WebDriverWait wait = phione.wait;

        //首先点击联系人，进入联系人列表
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain
                ("**/XCUIElementTypeButton[`name == \"联系人\"`]"))).click();

        //点击列表中的第一个用户
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeWindow[1]" +
                "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther[2]/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeTable/XCUIElementTypeCell[5]"))).click();

        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("MessagingApp.ConversationInputToolbar._attachmentButton"))).click();
        }catch(TimeoutException e){
            return "附件按钮消失";
        }

        //点击文件
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("文件"))).click();

        //选择文件内文件
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("从文件中选择"))).click();

        //点击直接发送
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("专门用来测试的文件"))).click();

        return "发送文件正常";
    }


//feature5：连续的发送信息
    public String sendMessageContinue(){
        Phione phione = new Phione();
        AppiumDriver driver = phione.driver;
        WebDriverWait wait = phione.wait;

        //首先点击联系人，进入联系人列表
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain
                ("**/XCUIElementTypeButton[`name == \"联系人\"`]"))).click();

        //点击列表中的第一个用户
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeWindow[1]" +
                "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther[2]/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeTable/XCUIElementTypeCell[5]"))).click();

        //定位文本框元素
        WebElement textBox=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("输入消息")));
        int i=1;
        for(;i<=8;i++){
            textBox.click();
            textBox.sendKeys("第"+i+"条消息");
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("MessagingApp.ConversationInputToolbar._sendButton"))).click();
        }

        //检查当前页面是不是存在对方设备不在线的情况
        try {
            if(driver.findElements(AppiumBy.iOSClassChain("**/XCUIElementTypeStaticText[`name == \"对方没有登录任何设备，消息发送失败，点击了解更多\"`]")).size()>0){
                return "对方没有登录任何设备发送失败";
            }
        }catch (NoSuchElementException e){
            System.out.println("发送消息成功");
        }

        //这个地方做一个发送的检测，看看是不是长时间发送不出去

        return "连续发送信息成功";
    }

//feature6：发送大型文件
    public String sendBigFile(){
        Phione phione = new Phione();
        AppiumDriver driver = phione.driver;
        WebDriverWait wait = phione.wait;

        //首先点击联系人，进入联系人列表
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain
                ("**/XCUIElementTypeButton[`name == \"联系人\"`]"))).click();

        //点击列表中的第一个用户
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeWindow[1]" +
                "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther[2]/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeTable/XCUIElementTypeCell[5]"))).click();

        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("MessagingApp.ConversationInputToolbar._attachmentButton"))).click();
        }catch(TimeoutException e){
            return "附件按钮消失";
        }

        //点击文件
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("文件"))).click();

        //选择文件内文件
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("从文件中选择"))).click();

        //点击直接发送
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("测试专用大文件.zip"))).click();

        //最后点击返回到主页面
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("icon back blue"))).click();
        System.out.println("大型文件已发送");
        return "大型文件已发送";
    }

//feature7:发送位置
    public String sendLocation(){
        Phione phione = new Phione();
        AppiumDriver driver = phione.driver;
        WebDriverWait wait = phione.wait;

        //首先点击联系人，进入联系人列表
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain
                ("**/XCUIElementTypeButton[`name == \"联系人\"`]"))).click();

        //点击列表中的第一个用户
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeWindow[1]" +
                "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther[2]/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeTable/XCUIElementTypeCell[5]"))).click();

        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("MessagingApp.ConversationInputToolbar._attachmentButton"))).click();
        }catch(TimeoutException e){
            return "附件按钮消失";
        }

        //点击位置
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("位置"))).click();

        //点击发送当前位置
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("发送我的当前位置"))).click();

        //最后点击返回到主页面
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("icon back blue"))).click();
        driver.quit();
        System.out.println("位置已发送，观察是否正确");
        return "位置已发送，观察是否正确";
    }


 //feature8:发送联系人名片
    public String sendContactCard(){
        Phione phione = new Phione();
        AppiumDriver driver = phione.driver;
        WebDriverWait wait = phione.wait;

        //首先点击联系人，进入联系人列表
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain
                ("**/XCUIElementTypeButton[`name == \"联系人\"`]"))).click();

        //点击列表中的第一个用户
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeWindow[1]" +
                "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther[2]/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeTable/XCUIElementTypeCell[5]"))).click();

        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("MessagingApp.ConversationInputToolbar._attachmentButton"))).click();
        }catch(TimeoutException e){
            return "附件按钮消失";
        }

        //点击联系人
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("联系人"))).click();

        //默认发送第二个联系人的名片
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]" +
                "/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeTable/XCUIElementTypeCell[2]"))).click();

        //点击弹出的确认框
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("确定"))).click();

        //最后点击返回到主页面
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("icon back blue"))).click();
        System.out.println("联系人名片已发送，观察是否正确");
        return "联系人名片已发送，观察是否正确";
    }


//feature9:发送语音消息
    public  String sendVoiceMessage(){
        Phione phione = new Phione();
        AppiumDriver driver = phione.driver;
        WebDriverWait wait = phione.wait;

        //首先点击联系人，进入联系人列表
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain
                ("**/XCUIElementTypeButton[`name == \"联系人\"`]"))).click();

        //点击列表中的第一个用户
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeWindow[1]" +
                "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther[2]/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeTable/XCUIElementTypeCell[5]"))).click();

        //识别麦克风按钮
        WebElement voiceButton=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("MessagingApp.ConversationInputToolbar._voiceMemoButton")));

        //创建长按手势
        Actions actions = new Actions(driver);

        // 执行长按操作
        actions.clickAndHold(voiceButton)
                .pause(Duration.ofSeconds(6))
                .release()
                .build()
                .perform();

        //最后点击返回到主页面
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("icon back blue"))).click();


        driver.quit();
        System.out.println("语音已发送，观察是否正确");
        return "语音已发送，观察是否正确";
    }


//feature10:发送表情，和表情包
    public String sendExpressionMessage() throws InterruptedException {
        Phione phione = new Phione();
        AppiumDriver driver = phione.driver;
        WebDriverWait wait = phione.wait;

        //首先点击联系人，进入联系人列表
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain
                ("**/XCUIElementTypeButton[`name == \"联系人\"`]"))).click();

        //点击列表中的第一个用户
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeWindow[1]" +
                "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther[2]/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeTable/XCUIElementTypeCell[5]"))).click();

        //首先发送小表情
        sendCommonExpression(wait);

        //发送表情包
        sendExpressionPackage(driver,wait);

        driver.quit();
        System.out.println("表情已发送，观察是否正确");
        return "表情已发送，观察是否正确";
    }

//发送小表情
    private void sendCommonExpression(WebDriverWait wait){
        //点击表情按钮
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId( "MessagingApp.ConversationInputToolbar._stickerButton"))).click();

        //底部导航栏选择表情选项
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeStaticText[`name == \"表情\"`]"))).click();

        //点击小表情
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("\uD83D\uDE02"))).click();

        //点击出现的发送
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("MessagingApp.ConversationInputToolbar._sendButton"))).click();
        System.out.println("常见表情已发送");
    }

//发送表情包
    private void sendExpressionPackage(AppiumDriver driver,WebDriverWait wait) throws InterruptedException {
        //发送表情后紧接着发送表情包，还在发送表情包页面，点击底部导航栏的表情包就行
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeStaticText[`name == \"表情包\"`]"))).click();
        Thread.sleep(1500);
        int mark=0;//为1就是已经添加过了

        //执行点击添加表情包的步骤
        try{
            driver.findElement(AppiumBy.iOSClassChain("**/XCUIElementTypeStaticText[`name == \"添加\"`]")).click();

            //执行添加
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeStaticText[`name == \"添加\"`]"))).click();

            //点击取消返回
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("取消"))).click();
        }
        catch (NoSuchElementException e){
            System.out.println("表情包已经添加过"); //点击一个表情发送
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeWindow[3]/XCUIElementTypeOther[1]/XCUIElementTypeOther/XCUIElementTypeOther[2]" +
                    "/XCUIElementTypeOther/XCUIElementTypeOther[1]/XCUIElementTypeScrollView/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeCollectionView/" +
                    "XCUIElementTypeCell[3]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeImage"))).click();
            mark=1;
        }

        if(mark==0){
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeWindow[3]/XCUIElementTypeOther[1]/XCUIElementTypeOther/XCUIElementTypeOther[2]" +
                    "/XCUIElementTypeOther/XCUIElementTypeOther[1]/XCUIElementTypeScrollView/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeCollectionView/" +
                    "XCUIElementTypeCell[3]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeImage"))).click();
        }

        System.out.println("表情包已发送");
        //最后点击返回到主页面
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("icon back blue"))).click();
    }
}
